package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类查询对应的菜品信息
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish,String name){
        //条件构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //条件1:菜品id和菜品套餐表id
        //条件2：菜品状态是否为1(起售状态)
        //条件3：模糊查询
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.like(name!=null,Dish::getName,name);
        wrapper.eq(Dish::getStatus,1);
        //添加排序条件
        wrapper.orderByDesc(Dish::getUpdateTime);
        //调用方法将条件放入
        List<Dish> list = dishService.list(wrapper);
        return R.success(list);
    }

    /**
     * 修改菜品信息，需要菜品和口味
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
       dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功~");
    }


    /**
     * 根据id查询对应菜品和口味回显数据至浏览器
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        //调用service层方法
        DishDto dishDto  = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    /**
     * 菜品分页查询，显示对应图片和菜品名称
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        log.info("第几页：{},每页显示条数：{}，条件：{}",page,pageSize,name);
        //封装
        Page<Dish> dishPageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPageInfo = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //模糊查询
        wrapper.like(name!=null,Dish::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Dish::getUpdateTime);
        //调用方法分页查询
        dishService.page(dishPageInfo,wrapper);
        //完成对象中的数据拷贝，参数1：被拷贝的对象 参数2：需要拷贝的对象 参数 3：拷贝时忽略哪些字段不拷贝
        BeanUtils.copyProperties(dishPageInfo,dishDtoPageInfo,"records");
        //获取当前页面数据
        List<Dish> dishList = dishPageInfo.getRecords();
        ArrayList<DishDto> dtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            //遍历循环获取菜品id
            Long categoryId = dish.getCategoryId();
            //通过菜品id获取所有菜品
            Category category = categoryService.getById(categoryId);
            //创建DishDto对象
            DishDto dishDto = new DishDto();
            //将dish菜品数据拷贝到dishDto
            BeanUtils.copyProperties(dish,dishDto);
            //这里封装所有菜品名称到dishDto中
            dishDto.setCategoryName(category.getName());
            //添加所有菜品名称到dishDto中
            dtoList.add(dishDto);
        }
        //分页查询（赋值dishDto中的数据到分页中）
        dishDtoPageInfo.setRecords(dtoList);
        return R.success(dishDtoPageInfo);
    }

    /**
     * 菜品管理：新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("新增菜品携带的参数：{}",dishDto.toString());
        //调用service层方法 注意：这里保存的是菜品的基本信息和口味信息
        dishService.saveWithFlavor(dishDto);
        return R.success("保存新菜品成功");
    }
}
